package com.wankuma.hcm.midlet.metwit;

import METwit;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

public class OpenUrlScreen extends List implements CommandListener {
	Command cmdOpen = new Command("開く", Command.OK, 0);
	Command cmdHatebu = new Command("はてなﾌﾞｯｸﾏｰｸに登録", Command.OK, 1);
	Command cmdCancel = new Command("取消", Command.CANCEL, 100);

	Displayable displayable;

	public OpenUrlScreen(Vector v, Displayable d) {
		super("URLを開く", List.IMPLICIT);

		this.displayable = d;

		for (int i = 0; i < v.size(); i++) {
			this.append((String) v.elementAt(i), null);
		}

		this.addCommand(cmdOpen);
		this.addCommand(cmdHatebu);
		this.addCommand(cmdCancel);
		this.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d) {
		METwit.getDisplay().setCurrent(displayable);
		if (c == cmdOpen) {
			try {
				METwit.getInstance().platformRequest(
						this.getString(this.getSelectedIndex()));
			} catch (Exception e) {
				Alerter.alert(e.toString());
			}
		} else if (c == cmdHatebu) {
			try {
				String url = 
					"http://b.hatena.ne.jp/add?mode=confirm&is_bm=1&url="
					+ UrlEncode.encode(this.getString(this.getSelectedIndex()));
				METwit.getInstance().platformRequest(url);
			} catch (Exception e) {
				Alerter.alert(e.toString());
			}
		}
	}
}
